package com.itheima.controller.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.sms.SmsCouponService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.pojo.SmsCoupon;

import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/coupon")
public class SmsCouponController {

    @DubboReference
    private SmsCouponService smsCouponService;


    //根据优惠券名称和类型分页获取优惠券列表
    @GetMapping("list")
    public AjaxResult list(@RequestParam(value = "name",required = false) String name,
                           @RequestParam(value = "type",required = false) Integer type,
                           @RequestParam(value = "pageSize") Integer pageSize,
                           @RequestParam(value = "pageNum") Integer pageNum){

        TableDataInfo page = smsCouponService.page(pageNum, pageSize, type, name);


        return AjaxResult.success(page);
    }



    //添加优惠券
    @PostMapping("/create")
    public AjaxResult save(@RequestBody SmsCoupon smsCoupon){

        smsCouponService.save(smsCoupon);

        return AjaxResult.success("添加成功");
    }

    //删除优惠券
    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id){

        smsCouponService.removeById(id);

        return AjaxResult.success("删除成功");
    }

    //优惠券的单个查询
    @GetMapping("/{id}")
    public AjaxResult findById(@PathVariable Long id){

        SmsCoupon smsCoupon = smsCouponService.getById(id);

        return AjaxResult.success(smsCoupon);
    }

        //修改优惠券
    @PostMapping("/update/{id}")
    public AjaxResult update(@PathVariable Long id,@RequestBody SmsCoupon smsCoupon){
            smsCoupon.setId(id);
            smsCouponService.update(smsCoupon);

        return AjaxResult.success("修改成功");
    }


}
